<template>
    <div id="friend-merchant-manage" class="w-h-100 pos-r flex-1 apply-container page-public-s" style="box-sizing: border-box;overflow:hidden;">
        <div class="w-h-100 ver-flex pos-a" style="box-sizing: border-box; padding: 6px;">
            <div v-if="$hasPer('ExaminationPaper:add') || $hasPer('ExaminationPaper:excel')" class="search-s page-public-s" style="box-sizing: border-box;">
                <el-card shadow="never" :body-style="{ paddingBottom: '0' }">
                    <el-form class="box-f" ref="queryForm" :inline="true">
                        <el-form-item>
                            <el-button v-if="$hasPer('ExaminationPaper:add')" @click="nodeClick('')" icon="ele-Plus" type="primary"> 新增 </el-button>
                            <el-button v-if="$hasPer('ExaminationPaper:excel')" @click="handleExport" icon="ele-Download"> 导出数据 </el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>

            <div style="box-sizing: border-box;" class="flex-1 pos-r">
                <div class="pos-a w-h-100" style="box-sizing: border-box;">
                    <el-card class="box-card w-h-100" shadow="never" style="margin-top: 16px;">
                        <div style="" class="w-h-100 ver-flex">
                            <div class="flex-1 pos-r">
                                <div class="pos-a w-h-100">
                                    <el-table ref="excelRef" :data="list" class="w-h-100" style="width: 100%" border v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.5)">
                                        <!-- <el-table-column type="index" label="序号" width="55" :show-overflow-tooltip="true" align="center" :index="indexMethod" /> -->
                                        <el-table-column min-width="200" label="考卷名称"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="i_Name" typeVal="input" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_Name">{{ scope.row.v_Name }}</span>
                                                    <span v-else> - </span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop="n_Min" label="考试时长(分钟)"  align="center" sortable />

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" label="奖惩措施"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="i_PIDM" typeVal="input" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_PIDM">{{ scope.row.v_PIDM }}</span>
                                                    <span v-else>-</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" label="培训计划名称"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="s_Plan" typeVal="select" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_CourseName">{{ scope.row.v_CourseName }}</span>
                                                    <span v-else> - </span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="200" :show-overflow-tooltip="true" label="选题方式"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="s_Method" typeVal="select" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.n_SelectionMethod === 0" style="color: #409EFF;">自定义选题</span>
                                                    <span v-if="scope.row.n_SelectionMethod === 1" style="color: #67C23A;">随机选题</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop="n_ScopeExcellentMin" label="优秀线(分数)"  align="center" sortable />

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop="n_ScopeQualifiedMin" label="合格线(分数)"  align="center" sortable />

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop=""  label="考卷描述" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="i_Description" typeVal="input" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_Description">{{ scope.row.v_Description }}</span>
                                                    <span v-else>--</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop="n_TrueOrFalseNum" label="判断题数量"  align="center" sortable />

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop="n_SingleChoiceNum" label="单选题数量"  align="center" sortable />

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" prop="n_MultipleChoiceNum" label="多选题数量"  align="center" sortable />

                                        <el-table-column min-width="180" :show-overflow-tooltip="true" prop="createdTime" label="创建时间"  align="center" sortable>
                                            <template #default="scope">
                                                <span v-if="scope.row.createdTime">{{ scope.row.createdTime }}</span>
                                                <span v-else>-</span>
                                            </template>
                                        </el-table-column>

                                        <el-table-column min-width="180" :show-overflow-tooltip="true" prop="updatedTime" label="更新时间"  align="center" sortable>
                                            <template #default="scope">
                                                <span v-if="scope.row.updatedTime">{{ scope.row.updatedTime }}</span>
                                                <span v-else>-</span>
                                            </template>
                                        </el-table-column>

                                        <el-table-column min-width="260" label="操作" align="center" fixed="right">
                                            <el-table-column :show-overflow-tooltip="true" min-width="260" align="center">
                                                <template #header>
                                                    <el-button class="icon-e" @click='searchClick' icon="ele-Search" size="small" type="primary" text> 查询 </el-button>
                                                    <el-button class="icon-e clear-bg" style="background-color: transparent !important;" size="small" @click="resetClick" icon="ele-Refresh" text> 重置 </el-button>
                                                </template>
                                                <template #default="scope">
                                                    <div class="operate-s">
                                                        <el-button @click="detailClick(scope.row)" class="icon-e" icon="ele-View" size="small" text type="success">详情</el-button>
                                                        <el-button v-if="$hasPer('ExaminationPaper:edit')" @click="nodeClick(scope.row)" class="icon-e" icon="ele-Edit" size="small" text type="primary">编辑</el-button>
                                                        <el-button v-if="$hasPer('ExaminationPaper:view')" @click="viewClick(scope.row)" class="icon-e" icon="ele-Checked" size="small" text type="success">试卷预览</el-button>
                                                        <el-button v-if="$hasPer('ExaminationPaper:del')" @click="deleteHandle(scope.row.id , getData)" class="icon-e" icon="ele-Delete" size="small" text type="danger">删除</el-button>
                                                    </div>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                    </el-table>
                                </div>
                            </div>
                            <div style="height: 30px;margin-top: 20px;" class="hr-flex-start">
								<el-pagination
									v-model:currentPage="pageNo"
									v-model:page-size="pageSize"
									:total="totalRows"
									:page-sizes="[10, 20, 50, 100]"
									small
									background
									@size-change="handleSizeChange"
									@current-change="handleCurrentChange"
									layout="total, sizes, prev, pager, next, jumper"
									class="w-h-100"
								/>
							</div>
                        </div>
                    </el-card>
                </div>
            </div>
        </div>
        <AddEditExaminationPaper
            :repairInfo="repairInfo"
            :isUseDialog="isUseDialog"
            @cancelUseClick="cancelUseClick"
            @controlUseClick="controlUseClick"
            @customClick="customClick"
            @updateData="getData"
        />
        <DetailExaminationPaper
            :repairInfo="detailInfo"
            :isUseDialog="detailDialog"
            @closeUseClick="closeUseClick"
        />
        <PaperExaminationPaper
            :repairInfo="paperInfo"
            :isUseDialog="paperDialog"
            @cancelPaperClick="cancelPaperClick"
        />
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, onMounted , reactive , toRaw } from 'vue';
import getTableData from '/@/utils/getTableData';
import { 
    OmsEduExaminationPaperPage , 
    OmsEduExaminationPaperGeneratePaper , 
    OmsEduExaminationPaperDelete , 
    EduTrainingPlanList ,
} from '/@/api/centerControl/sms';
import AddEditExaminationPaper from '../component/sms/add-edit-ExaminationPaper.vue';
import DetailExaminationPaper from '../component/sms/detail-ExaminationPaper.vue';
import PaperExaminationPaper from '../component/sms/paper-ExaminationPaper.vue';
import { ElMessageBox , ElMessage } from 'element-plus';
import SearchModel from '../component/search-model.vue';
import { exportToExcel } from '/@/utils/exportToExcel';

export default defineComponent( {
    name: 'ExaminationPaper',
    components: {
        AddEditExaminationPaper,
        DetailExaminationPaper,
        PaperExaminationPaper,
        SearchModel,
    },
    setup(props){
        const isUseDialog = ref(); // 新增/编辑弹出框隐藏/显示
        const repairInfo = ref(); // 传递子组件数据
        const detailInfo = ref();
        const detailDialog = ref(false);
        const paperInfo = ref();
        const paperDialog = ref(false);

        /*** nodeClick 新增/编辑点击事件 */
        const nodeClick = (val:string | object) => {
            val !== '' ? repairInfo.value = JSON.stringify(toRaw(val)) : repairInfo.value = ''
            isUseDialog.value = true
        };

        /*** deleteHandle 删除数据事件 */
        const deleteHandle = async (val: String | Number, updateFun: Function) => {
            ElMessageBox.confirm('请确定删除此条数据？','提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
            .then(async() => {
                let param = {id : val};
                try {
                    let res:any = await OmsEduExaminationPaperDelete(param)
                    if(res.success){
                        updateFun()
                    }
                } catch (err) { return }
            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '已取消',
                })
            })
        };

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            isUseDialog.value = false
        };

        /*** customClick 自定义考题提交事件 */
        const customClick = () => {
            isUseDialog.value = false
        }

        /*** controlUseClick 提交事件 */
        const controlUseClick = () => {
            isUseDialog.value = false
        };

        /*** onSuccess 文件上传成功时的钩子函数 */
        const onSuccess = (item:any) => {
            console.log(item,'item')
        };

        /*** detailClick 详情按钮点击事件 */
        const detailClick = (val: any) => {
            detailInfo.value = JSON.stringify(val);
            detailDialog.value = true;
        }

        /*** closeUseClick 详情关闭事件 */
        const closeUseClick = () => {
            detailDialog.value = false;
        }

        /*** viewClick 考卷预览 */
        const viewClick = async(val: any) => {
            let param = {id: val.id}
            let res: any = await OmsEduExaminationPaperGeneratePaper(param);
            if(res.success) {
                console.log(res.data,'res.data12')
                let setObj: any = {};
                let totalNum = 0;
                res.data.map((i: any) => { 
                    totalNum += i.n_Score;
                });
                setObj.titleCon = val.v_CourseName;
                setObj.paperName = val.v_Name;
                setObj.paperArr = res.data;
                setObj.totalNum = totalNum;
                paperInfo.value = JSON.stringify(setObj);
                paperDialog.value = true;
            }
        }

        /*** cancelPaperClick 试卷预览关闭事件 */
        const cancelPaperClick = () => {
            paperDialog.value = false;
        }
        
        /*** ================================= 查询类数据操作 =================================  */
        let searchObj:any = reactive({});
        const basicsInfo: any = ref([]);
        const searInfo: any = ref();

        /*** 获取需要查询的数据 */
        const searchHandel = (val: any) => {
            searInfo.value = val;
        }

        /*** searchClick 查询按钮 */
        const searchClick = () => {
            let newObj = Object.keys(searInfo.value);
            if(newObj.length !== 0){
                newObj.map((i:any) => {
                    searchObj[i] = searInfo.value[i];
                })
            }
        };

        /*** resetClick 重置按钮 */
        const resetClick = () => {
            searInfo.value = {};
            let newObj = Object.keys(searchObj);
            if(newObj.length !== 0){
                newObj.map((i:any) => {
                    searchObj[i] = ''
                })
            }
            initInfoFun();
        };

        let planArr: any = [];
        /*** getPlanList 获取培训计划列表 */
        const getPlanList = async() => {
            let param = {}
            let res:any = await EduTrainingPlanList(param);
            if(res.success && res.data.length !==0){
                res.data.map((item: any) => {
                    item.label = item.v_CourseName;
                    item.value = item.id;
                });
                planArr = res.data;
            }
        };
        
        /*** 初始化数据 */
        const initInfoFun = () => {
            basicsInfo.value = [{
                    typeId: 'i_Name', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'v_Name', // 查询字段 --- 必填v
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '考卷名称', // placeholder数据 --- 必填
                    defaultProp: '', // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: 'burl', // 事件类型(如burl/change等) --- 选填
                    defaultEvent: 'nameEvent', // 事件名称 --- 选填
                },{
                    typeId: 'i_PIDM', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'v_PIDM', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '奖惩措施', // placeholder数据
                    defaultProp: '', // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: 'burl', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: 'pidmEvent', // 事件名称 --- 选填
                },{
                    typeId: 's_Plan', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'l_TrainingPlanId', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '培训计划', // placeholder数据
                    defaultProp: planArr, // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: 'change', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: 'planChange', // 事件名称 --- 选填
                },{
                    typeId: 's_Method', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'N_SelectionMethod', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '选题方式', // placeholder数据
                    defaultProp: [
                        {label: '自定义', value: 0},
                        {label: '随机', value: 1}
                    ], // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: 'change', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: 'methodChange', // 事件名称 --- 选填
                },{
                    typeId: 'i_Description', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'v_Description', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '考卷描述', // placeholder数据
                    defaultProp: '', // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: 'burl', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: 'descriptioncEvent', // 事件名称 --- 选填
                },
            ]
        }
        
        onMounted(async () => {
            await getPlanList();
            initInfoFun();
        });
        /*** ================================= 查询类数据操作 =================================  */

        /*** ================================= 导出数据函数操作 ================================= */
        const excelRef: any = ref(null);
        const handleExport = () => {
            if(excelRef){
                if(excelRef.value.data.length !== 0){
                    let tableExcel: any = [];
                    let tableArr = excelRef.value.data;
                    tableArr.map((item: any) => {
                        let tableObj: any = {};
                        tableObj['考卷名称'] = item.v_Name ? item.v_Name : '-';
                        tableObj['考试时长'] = item.n_Min ? item.n_Min : '-';
                        tableObj['奖惩措施'] = item.v_PIDM ? item.v_PIDM : '-';
                        tableObj['培训计划名称'] = item.v_CourseName ? item.v_CourseName : '-';
                        tableObj['选题方式'] = item.n_SelectionMethod === 0 ? '自定义选题' : '随机选题';
                        tableObj['优秀线'] = item.n_ScopeExcellentMin ? item.n_ScopeExcellentMin : '-';
                        tableObj['合格线'] = item.n_ScopeQualifiedMin ? item.n_ScopeQualifiedMin : '-';
                        tableObj['考卷描述'] = item.v_Description ? item.v_Description : '-';
                        tableObj['判断题数量'] = item.n_TrueOrFalseNum;
                        tableObj['单选题数量'] = item.n_SingleChoiceNum;
                        tableObj['多选题数量'] = item.n_MultipleChoiceNum;
                        tableExcel.push(tableObj);
                    });
                    exportToExcel(tableExcel,'试卷编制.xlsx');
                }else{
                    ElMessage({
                        type: 'error',
                        message: '暂无数据导出',
                    })
                }
            }
        }
        /*** ================================= 导出数据函数操作 ================================= */

        return {
            ...getTableData(OmsEduExaminationPaperPage,{},searchObj),
            isUseDialog,
            cancelUseClick,
            controlUseClick,
            customClick,
            nodeClick,
            repairInfo,
            deleteHandle,
            resetClick,
            searchClick,
            onSuccess,
            detailClick,
            detailInfo,
            detailDialog,
            closeUseClick,
            viewClick,
            cancelPaperClick,
            paperDialog,
            paperInfo,

            // 导出Excel
            excelRef,
            handleExport,

            // 查询事件
            basicsInfo,
            searchHandel,
        }
    },
} )
</script>
<style lang="scss"> @import '../style/public.scss'; </style>
<style scoped lang="scss">
    #friend-merchant-manage {
        .box-f{
            :deep(.el-form-item__label){
                padding: 0;
            }
        }
        :deep(.el-card__body) {
            height: 100%;
        }
        :deep(.el-card){
            border: none !important;
        }
        .upload-demo{
            margin-left: 12px;
            margin-top: 2px;
        }
        ul {

            list-style: none;

            li {
                cursor: pointer;

                &:hover {
                    color: red;
                }
            }
        }
        .icon-e{
            :deep(.el-icon){
                margin-right: 0px!important;
            }
        }
    }
.clear-bg{
    background-color: transparent !important;
}
</style>